<template>
  <div class="app-container">
    <el-card class="content-edit">
      <div slot="header">
        <span>模板消息配置</span>
        <el-button type="primary" size="small" style="float: right;" @click="onSubmit">保存</el-button>
      </div>
      <el-form ref="form" :model="form" label-width="160px">
        <el-form-item label="新用户注册">
          <el-switch v-model="form.registerTplStatus"/>
          <el-input :disabled="!form.registerTplStatus" v-model="form['registerTpl']"/>
        </el-form-item>
        <el-form-item label="预约到期">
          <el-switch v-model="form.appointmentExpireTplStatus"/>
          <el-input :disabled="!form.appointmentExpireTplStatus" v-model="form['appointmentExpireTpl']"/>
        </el-form-item>
        <el-form-item label="订单支付">
          <el-switch v-model="form.payTplStatus"/>
          <el-input :disabled="!form.payTplStatus" v-model="form['payTpl']"/>
        </el-form-item>
        <el-form-item label="订单取消">
          <el-switch v-model="form.revokeTplStatus"/>
          <el-input :disabled="!form.revokeTplStatus" v-model="form['revokeTpl']"/>
        </el-form-item>
        <el-form-item label="订单被接">
          <el-switch v-model="form.receiptTplStatus"/>
          <el-input :disabled="!form.receiptTplStatus" v-model="form['receiptTpl']"/>
        </el-form-item>
        <el-form-item label="订单未支付">
          <el-switch v-model="form.notPayTplStatus"/>
          <el-input :disabled="!form.notPayTplStatus" v-model="form['notPayTpl']"/>
        </el-form-item>
        <el-form-item label="订单过期">
          <el-switch v-model="form.expireTplStatus"/>
          <el-input :disabled="!form.expireTplStatus" v-model="form['expireTpl']"/>
        </el-form-item>
        <el-form-item label="订单退款">
          <el-switch v-model="form.refundTplStatus"/>
          <el-input :disabled="!form.refundTplStatus" v-model="form['refundTpl']"/>
        </el-form-item>
        <el-form-item label="新订单提醒">
          <el-switch v-model="form.orderRemindTplStatus"/>
          <el-input :disabled="!form.orderRemindTplStatus" v-model="form['orderRemindTpl']"/>
        </el-form-item>
        <!--<el-form-item>
          <el-button type="primary" @click="onSubmit">保存</el-button>
          <el-button @click="$router.back()">取消</el-button>
        </el-form-item>-->
      </el-form>
    </el-card>
  </div>
</template>

<script>
  export default {
    name: 'CustomerServiceTelephone',
    data() {
      return {
        form: {
          // 用户注册
          registerTpl: '',
          registerTplStatus: false,
          // 支付通知
          payTpl: '',
          payTplStatus: false,
          // 接单通知
          receiptTpl: '',
          receiptTplStatus: false,
          // 退款通知
          refundTpl: '',
          refundTplStatus: false,
          // 订单未支付
          notPayTpl: '',
          notPayTplStatus: false,
          // 订单取消
          revokeTpl: '',
          revokeTplStatus: false,
          // 订单过期
          expireTpl: '',
          expireTplStatus: false,
          // 预约过期
          appointmentExpireTpl: '',
          appointmentExpireTplStatus: false,
          // 新订单提醒
          orderRemindTpl: '',
          orderRemindTplStatus: false
        }
      }
    },
    mounted(){
      this.loadData();
    },
    methods: {
      // 加载默认头像列表
      loadData(){
        (async () => {
          const result = await this.$service.applicationGetWxMessageConfig();
          if (result.code === 0) {
            const temp = result.data;
            temp.registerTplStatus = !!temp.registerTplStatus;
            temp.payTplStatus = !!temp.payTplStatus;
            temp.receiptTplStatus = !!temp.receiptTplStatus;
            temp.refundTplStatus = !!temp.refundTplStatus;
            temp.notPayTplStatus = !!temp.notPayTplStatus;
            temp.revokeTplStatus = !!temp.revokeTplStatus;
            temp.expireTplStatus = !!temp.expireTplStatus;
            temp.appointmentExpireTplStatus = !!temp.appointmentExpireTplStatus;
            temp.orderRemindTplStatus = !!temp.orderRemindTplStatus;
            this.form = Object.assign(this.form, result.data || {})
          }
        })();
      },
      // 提交
      onSubmit() {
        this.$refs.form.validate(valid => {
          if (valid) {
            (async () => {
              const params = Object.assign({}, this.form);
              console.log(params)
              params.registerTplStatus = (params.registerTplStatus ? 1 : 0);
              params.payTplStatus = (params.payTplStatus ? 1 : 0);
              params.receiptTplStatus = (params.receiptTplStatus ? 1 : 0);
              params.refundTplStatus = (params.refundTplStatus ? 1 : 0);
              params.notPayTplStatus = (params.notPayTplStatus ? 1 : 0);
              params.revokeTplStatus = (params.revokeTplStatus ? 1 : 0);
              params.expireTplStatus = (params.expireTplStatus ? 1 : 0);
              params.appointmentExpireTplStatus = params.appointmentExpireTplStatus ? 1 : 0;
              params.orderRemindTplStatus = params.orderRemindTplStatus ? 1 : 0;
              const result = await this.$service.applicationSaveWxMessageConfig(params);
              if (result.code === 0) {
                this.$message.success('操作成功~');
                this.loadData();
              } else {
                this.$message.error(result.msg);
              }
            })();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }
</script>
